using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Spell: LoadableJSONObject{
	public List<string> spellList = new List<string>{"Sorceror/Wizard"};
	
	public List<int> spellLevel = new List<int>{0};
	
	public string school = "Universal";
	
	public string castingTime = "1 standard action";
	public string components = "V,S,M,F";
	public string range = "touch";
	
	public string duration = "instantaneous";
	public string save = "Will negates";
	public bool spellResistance = true;
	
	public Spell()
	{
		
	}
	
	public bool MatchesListAndLevel(string list, int level)
	{
		for(int i =0; i < spellList.Count; i++)
		{
			if(spellList[i] == list && spellLevel[i] == level)
				return true;
		}
		return false;
	}
	
	public int GetLevelFromList(string List)
	{
		for(int i = 0; i < spellList.Count; i++)
		{
			if(spellList[i] == List)
			{
				return spellLevel[i];
			}
		}
		return -1;
	}
	
	public override void Load(string json, string file)
	{
		fileName = file;
		if(json != null)
		{
			Dictionary<string,object> data = MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
			
			description = data["description"] as string;
			name = data["name"] as string;
			spellList = (MiniJSON.Json.Deserialize(data["spellList"] as string) as List<object>).ConvertAll<string>(o => o as string);
			spellLevel = (MiniJSON.Json.Deserialize(data["spellLevel"] as string) as List<object>).ConvertAll<int>(o => int.Parse(o as string));
			school = data["school"] as string;
			castingTime = data["castingTime"] as string;
			components = data["components"] as string;
			range = data["range"] as string;
			duration = data["duration"] as string;
			save = data["save"] as string;
			spellResistance = bool.Parse(data["spellResistance"] as string);
		}
	}
	
	public override string ToJSON()
	{
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("name", name);
		data.Add("description", description);
		data.Add("spellList", MiniJSON.Json.Serialize(spellList));
		data.Add("spellLevel", MiniJSON.Json.Serialize(spellLevel));
		data.Add("school", school);
		data.Add("castingTime", castingTime);
		data.Add("components", components);
		data.Add("range", range);
		data.Add("duration", duration);
		data.Add("save", save);
		data.Add("spellResistance", spellResistance.ToString());
		
		return MiniJSON.Json.Serialize(data);
	}
	
	public Spell DeepCopy()
	{
		Spell a = new Spell();
		a.Load(ToJSON(), fileName);
		return a;
	}
}
